package com.huyanqi.front.rest.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.huyanqi.core.Page;
import com.huyanqi.core.RetdCodeType;
import com.huyanqi.core.RetdObj;
import com.huyanqi.core.service.impl.BaseFrontServiceImpl;
import com.huyanqi.dao.PraiseDAO;
import com.huyanqi.dao.ShopDAO;
import com.huyanqi.entity.Praise;
import com.huyanqi.entity.Shop;
import com.huyanqi.front.rest.service.ShopRestService;

@Service("ShopRestService")
public class ShopRestImpl extends BaseFrontServiceImpl<Shop,Long, ShopDAO> implements ShopRestService{

	@Override
	@Resource(name="ShopDAO")
	public void setEntityDao(ShopDAO entityDao) {
		this.entityDao=entityDao;
	}
	
	private PraiseDAO praiseDao;
	@Resource(name="PraiseDAO")
	public void setPraiseDao(PraiseDAO praiseDao) {
		this.praiseDao = praiseDao;
	}

	@Override
	public RetdObj<List<Shop>> findShopBySetId(Long setid, Integer pageSize,Integer pageNum) {
		RetdObj<List<Shop>> retdObj=new RetdObj<List<Shop>>();
		Page<Shop> page = new Page<Shop>();
		page.setFirstResult((pageNum-1)*pageSize);
		page.setPageSize(pageSize);
		try {
			List<Shop> list = entityDao.findPage(page, "SELECT s FROM Shop s,Shopactivity sa WHERE s.sId = sa.aShop and sa.aSet = ? GROUP BY s.sId ORDER BY sa.aTime desc", setid).getResult();
			if(list.size()>0){
				retdObj.setRealObj(list);
				retdObj.setCode(RetdCodeType.PASS_OK);
			}else{
				retdObj.setCode(RetdCodeType.PASS_NODATA);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retdObj;
	}
	
	@Override
	public RetdObj<Shop> findShopBySid(Long shopid,Long uid) {
		RetdObj<Shop> obj = new RetdObj<Shop>();
		Shop s = entityDao.executeQuery("FROM Shop WHERE sId = ?", shopid).get(0);
		if(s!=null){
			List<Praise> list = praiseDao.executeQuery("FROM Praise WHERE pUid = ? AND pShopid = ?", new Object[]{uid,shopid});
			if(list.size()>0){
				s.setIsPraise(true);
			}
			obj.setRealObj(s);
			obj.setCode(RetdCodeType.PASS_OK);
		}else{
			obj.setCode(RetdCodeType.PASS_NODATA);
		}
		
		return obj;
	}

}
